package com.jams.goodox.activity.serve;

import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;

import com.hangzhou.jin.customview.LoadView;
import com.jams.goodox.BaseActivity;
import com.jams.goodox.R;
import com.jams.goodox.constant.AppConfig;
import com.jams.goodox.entity.RentingBean;
import com.jams.goodox.entity.resp.RentingHouseResp;
import com.jams.goodox.net.RetrofitCallbackListener;
import com.jams.goodox.net.services.LifeService;
import com.jams.goodox.utils.CollectionUtils;
import com.jams.goodox.utils.DelayUtils;
import com.jams.goodox.utils.StringUtils;
import com.jams.goodox.utils.StyleUtil;
import com.jams.goodox.widget.RefreshRecyclerView;
import com.jams.goodox.widget.SimpleDividerItemDecoration;
import com.jams.goodox.widget.mannger.RefreshLinearLayoutManager;
import com.jams.library.commonAdapter.RecycleCommonAdapter;
import com.jams.library.commonAdapter.RecycleCommonViewHolder;
import com.jams.library.listener.OnRecycleItemClickListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import retrofit2.Call;

/**
 * 官方租房列表
 * <p>
 * create by 顾佳佳 on 2017/05/04
 */
public class OfficialRentingActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener {

    @Bind(R.id.empty_view)
    LoadView emptyView;
    @Bind(R.id.rcv_official_renting)
    RefreshRecyclerView rcvOfficialRenting;
    @Bind(R.id.sfl_official_renting)
    SwipeRefreshLayout sflOfficialRenting;

    private List<RentingBean> dataList;
    private int curPage;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_official_renting;
    }

    @Override
    protected void init() {
        StyleUtil.titleBackKey(this, getString(R.string.official_renting));
        dataList = new ArrayList<>();
        sflOfficialRenting.setOnRefreshListener(this);
        rcvOfficialRenting.setLayoutManager(new RefreshLinearLayoutManager(this));
        rcvOfficialRenting.setHasFixedSize(true);
        rcvOfficialRenting.setItemAnimator(new DefaultItemAnimator());
        rcvOfficialRenting.setAdapter(new RecycleCommonAdapter<RentingBean>(this, dataList, R.layout.official_renting_item) {
            @Override
            public void convert(RecycleCommonViewHolder helper, RentingBean item, int position) {
                setData(helper,item,position);
            }
        });
        rcvOfficialRenting.setLoadDataListener(new RefreshRecyclerView.LoadDataListener() {
            @Override
            public void onLoadMore() {
                getData();
            }
        });
        rcvOfficialRenting.addItemDecoration(new SimpleDividerItemDecoration(this));
        onRefresh();
    }


    //设置数据
    private void setData(RecycleCommonViewHolder helper, final RentingBean item, int position){
        helper.setSimpleDraweeView(R.id.sdv_official_item,item.getAnnex().getUrlPath())
                .setText(R.id.official_renting_title,item.getTitle())
                .setText(R.id.tv_official_time,item.getCreatingTime())
                .setText(R.id.tv_official_renting_money, StringUtils.getDoubleToNatureString(item.getLowerPrice())+"元/月");
                if(item.getRentType().equals("整租")){//整租
                    helper.setViewVisible(R.id.tv_official_type,true)
                            .setText(R.id.tv_official_type,"整租");
                    helper.setViewVisible(R.id.tv_official_room_num,false);
                }else if(item.getRentType().equals("合租")){//合租
                    helper.setViewVisible(R.id.tv_official_type,true)
                            .setText(R.id.tv_official_type,"合租");
                    helper.setViewVisible(R.id.tv_official_room_num,item.getRoomCount()>0)
                            .setText(R.id.tv_official_room_num,item.getRoomCount()+"室");
                }
        helper.setOnItemClickListener(new OnRecycleItemClickListener() {
            @Override
            public void itemClick() {
                Intent intent = new Intent(OfficialRentingActivity.this,OfficialRentingDetailActivity.class);
                intent.putExtra(OfficialRentingDetailActivity.RENTING,item);
                qStartActivity(intent);
            }
        });
    }
    @Override
    public void onRefresh() {
        DelayUtils.doAfterDelay(100, new DelayUtils.DelayDoListener() {
            @Override
            public void doAction() {
                sflOfficialRenting.setRefreshing(true);
                curPage = 1;
                getData();
            }
        });
    }

    //获取官方租房列表
    private void getData() {
        enqueue(createCallService(LifeService.class).getOfficialRentingList(curPage, AppConfig.PagSize), new RetrofitCallbackListener<RentingHouseResp>() {
            @Override
            public void onSuccess(Call<RentingHouseResp> call, RentingHouseResp result) {
                sflOfficialRenting.setRefreshing(false);
                rcvOfficialRenting.loadMoreComplete();
                if (result.getData() != null) {
                    if (curPage == 1) {
                        dataList.clear();
                    }
                    if (result.getData().getPageInfo() == null) {
                        rcvOfficialRenting.setCanLoadMore(false);
                    } else {
                        rcvOfficialRenting.setCanLoadMore(result.getData().getPageInfo().isHasMore());
                    }
                    dataList.addAll(result.getData().getResultList());
                    curPage++;
                    rcvOfficialRenting.notifyData();
                }
                if (!CollectionUtils.hasData(dataList)) {
                    emptyView.loadCompleteNoData(R.mipmap.holder_no_room,"暂无房源");
                } else {
                    emptyView.loadComplete();
                }
            }

            @Override
            public void onError(String msg) {
                sflOfficialRenting.setRefreshing(false);
                rcvOfficialRenting.loadMoreComplete();
                if (!CollectionUtils.hasData(dataList)) {
                    emptyView.loadCompleteNoData(R.mipmap.holder_no_room,"暂无房源");
                } else {
                    emptyView.loadComplete();
                }
            }
        });
    }
}
